//
//  JsonParseHelper.m
//  ERP
//
//  Created by goldeney on 15/1/8.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "JsonParseHelper.h"
#import "GDataXMLNode.h"
#import "AESCrypt.h"

@implementation JsonParseHelper

#pragma mark -
#pragma mark 获取methodName+Result里面的内容(自定义数据解析) NSMutableArray

+ (NSArray*)soapMessageResultXmlDocument:(id)data serviceMethodName:(NSString*)methodName
{
    NSString *resultXmlString = [self soapMessageResultXml:data serviceMethodName:methodName];
    NSString *decryptData = [AESCrypt decrypt:resultXmlString];
    return [self jsonParseMethodNameResult:decryptData];
}

#pragma mark -
#pragma mark 解析json数据内容

+ (NSArray*)jsonParseMethodNameResult:(NSString*)responseString
{
    //判断数据是否异常
    if (!responseString) {
        NSLog(@"An error happened while Unable to resolve data.");
        return nil;
    }
    
    //去除掉首尾的空白字符和换行字符
    responseString = [responseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
    
    NSData *decryptData= [responseString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    
    id jsonObject = [NSJSONSerialization JSONObjectWithData:decryptData options:NSJSONReadingAllowFragments error:&error];
    if ([jsonObject isKindOfClass:[NSDictionary class]]){
        NSDictionary *dictionary = (NSDictionary *)jsonObject;
        return [NSArray arrayWithObjects:dictionary, nil];
        
    } else if ([jsonObject isKindOfClass:[NSArray class]]){
        NSArray *nsArray = (NSArray *)jsonObject;
        return nsArray;
        
    } else {
        NSLog(@"An error happened while deserializing the JSON data.");
        return nil;
    }
}

#pragma mark -
#pragma mark 获取methodName+Result里面的内容

+ (NSString*)soapMessageResultXml:(id)data serviceMethodName:(NSString*)methodName{
    GDataXMLDocument *document=[self xmlDocumentObject:data nodeName:nil];
    if (document) {
        GDataXMLElement* rootNode = [document rootElement];
        
        NSString *searchStr=[NSString stringWithFormat:@"%@Result",methodName];
        NSString *MsgResult=@"";
        NSArray *result=[rootNode children];
        
        while ([result count]>0) {
            NSString *nodeName=[[result objectAtIndex:0] name];
            if ([nodeName isEqualToString: searchStr]) {
                MsgResult=[[result objectAtIndex:0] stringValue];
                break;
            }
            result=[[result objectAtIndex:0] children];
        }
        return MsgResult;
    }
    return @"";
}


#pragma mark -
#pragma mark xml操作辅助方法
+(GDataXMLDocument*)xmlDocumentObject:(id)data nodeName:(NSString*)name{
    NSError *error=nil;
    GDataXMLDocument *document=nil;
    if (name) {
        NSString *xml=nil;
        if ([data isKindOfClass:[NSString class]]){
            xml=(NSString*)data;
        }else{
            xml=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        }
        if (xml) {
            xml=[xml stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"xmlns=\"%@\"",name] withString:@""];
            
            document=[[GDataXMLDocument alloc] initWithXMLString:xml options:0 error:&error];
        }
    }else{
        if ([data isKindOfClass:[NSString class]]) {
            document=[[GDataXMLDocument alloc] initWithXMLString:data options:0 error:&error];
        }else{
            document=[[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
        }
    }
    if (error||document==nil) {
        return nil;
    }
    //[document setCharacterEncoding:@"uft-8"];
    return document;
}

@end
